package com.zcl.customcontrols;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;


@SuppressWarnings("unused")
public class TabWidgetEx extends LinearLayout implements OnFocusChangeListener {
	private Drawable mDividerDrawable = null; // 各个标签中间的分割条
	private boolean  mDrawBottomStrips = true; // 是否要绘制tab控件下面的长条
    private Drawable mBottomLeftStrip; // tab控件下边长条的左边
    private Drawable mBottomRightStrip; // tab控件下边长条的右边
    private boolean  mStripMoved = true; // 被选中的标签是否改变
    private int 	 mSelectedTab = 0; // 当前选中的标签
    private OnTabSelectionChanged mSelectionChangedListener; // 当前tab被改变时触发该事件
	
	public TabWidgetEx(Context context) {
		super(context);
		initTabWidget();
	}

	public TabWidgetEx(Context context, AttributeSet attrs) {
		super(context, attrs);
		initTabWidget();
	}
	
    public void setDividerDrawable(Drawable drawable) {
        mDividerDrawable = drawable;
    }
    
    /**
     * 设置标签页下面的横条
     */
    public void initTabWidget() {
        setOrientation(LinearLayout.HORIZONTAL);

        final Context context = getContext();
        final Resources resources = context.getResources();
        

        // Use modern color scheme for Eclair and beyond
   /*     mBottomLeftStrip = resources.getDrawable(
              R.drawable.tab_bottom_left);
        mBottomRightStrip = resources.getDrawable(
              R.drawable.tab_bottom_right);*/


        // Deal with focus, as we don't want the focus to go by default
        // to a tab other than the current tab
        setFocusable(true);
        setOnFocusChangeListener(this);
    }
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mStripMoved = true;
        super.onSizeChanged(w, h, oldw, oldh);
    }
    
    @Override
    public void addView(View child){
    	if (child.getLayoutParams() == null) {
            final LinearLayout.LayoutParams lp = new LayoutParams(
                    0,
                    ViewGroup.LayoutParams.FILL_PARENT, 1.0f);
            lp.setMargins(0, 0, 0, 0);
            child.setLayoutParams(lp);
        }

        // Ensure you can navigate to the tab with the keyboard, and you can touch it
        child.setFocusable(true);
        child.setClickable(true);

        // If we have dividers between the tabs and we already have at least one
        // tab, then add a divider before adding the next tab.
        if (mDividerDrawable != null && getTabCount() > 0) {
            ImageView divider = new ImageView(this.getContext());
            final LinearLayout.LayoutParams lp = new LayoutParams(
                    mDividerDrawable.getIntrinsicWidth(),
                    LayoutParams.FILL_PARENT);
            lp.setMargins(0, 0, 0, 0);
            divider.setLayoutParams(lp);
            divider.setBackgroundDrawable(mDividerDrawable);
            super.addView(divider);
        }
        super.addView(child);

        // than potentially interfere with the view's listener
        child.setOnClickListener(new TabClickListener(getTabCount() - 1));
        child.setOnFocusChangeListener(this); 	
    }
    
    public int getTabCount() {
        int children = getChildCount();

        // If we have dividers, then we will always have an odd number of
        // children: 1, 3, 5, ... and we want to convert that sequence to
        // this: 1, 2, 3, ...
        if (mDividerDrawable != null) {
            children = (children + 1) / 2;
        }
        return children;
    }
    
    @Override
    public void childDrawableStateChanged(View child) {
        if (child == getChildTabViewAt(mSelectedTab)) {
            // To make sure that the bottom strip is redrawn
            invalidate();
        }
        super.childDrawableStateChanged(child);
    }

	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
/*		
        // If the user specified a custom view for the tab indicators, then
        // do not draw the bottom strips.
        if (!mDrawBottomStrips) {
            // Skip drawing the bottom strips. 
            return;
        }

        View selectedChild = getChildTabViewAt(this.mSelectedTab);

        mBottomLeftStrip.setState(selectedChild.getDrawableState());
        mBottomRightStrip.setState(selectedChild.getDrawableState());
        

        if (mStripMoved) {
            Rect selBounds = new Rect(); // Bounds of the selected tab indicator
            selBounds.left = selectedChild.getLeft() + 5;
            selBounds.right = selectedChild.getRight() - 5;
            //selBounds.left = selBounds.right;
            final int myHeight = getHeight();
            mBottomLeftStrip.setBounds(
                    Math.min(0, selBounds.left
                                 - mBottomLeftStrip.getIntrinsicWidth()),
                    //myHeight - mBottomLeftStrip.getIntrinsicHeight(),
                    0,
                    selBounds.left,
                    //getHeight());
                    mBottomLeftStrip.getIntrinsicHeight());
            mBottomRightStrip.setBounds(
                    selBounds.right,
                    //myHeight - mBottomRightStrip.getIntrinsicHeight(),
                    0,
                    Math.max(getWidth(),
                            selBounds.right + mBottomRightStrip.getIntrinsicWidth()),
                    //myHeight);
                    mBottomRightStrip.getIntrinsicHeight());
            mStripMoved = false;
        }

        mBottomLeftStrip.draw(canvas);
        mBottomRightStrip.draw(canvas);*/
	}
	
    public View getChildTabViewAt(int index) {
        // If we are using dividers, then instead of tab views at 0, 1, 2, ...
        // we have tab views at 0, 2, 4, ...
        if (mDividerDrawable != null) {
            index *= 2;
        }
        return getChildAt(index);
    }
    
    public void setCurrentTab(int index) {
        if (index < 0 || index >= getTabCount()) {
            return;
        }

        getChildTabViewAt(mSelectedTab).setSelected(false);
        mSelectedTab = index;
        getChildTabViewAt(mSelectedTab).setSelected(true);
        mStripMoved = true;
    }
    
    void setTabSelectionListener(OnTabSelectionChanged listener) {
        mSelectionChangedListener = listener;
    }
    
    public void focusCurrentTab(int index) {
        final int oldTab = mSelectedTab;

        // set the tab
        setCurrentTab(index);

        // change the focus if applicable.
        if (oldTab != index) {
            getChildTabViewAt(index).requestFocus();
        }
    }
    
    public void onFocusChange(View v, boolean hasFocus) {
        if (v == this && hasFocus) {
            getChildTabViewAt(mSelectedTab).requestFocus();
            return;
        }

        if (hasFocus) {
            int i = 0;
            int numTabs = getTabCount();
            while (i < numTabs) {
                if (getChildTabViewAt(i) == v) {
                    setCurrentTab(i);
                    mSelectionChangedListener.onTabSelectionChanged(i, false);
                    break;
                }
                i++;
            }
        }
    }
    
 // registered with each tab indicator so we can notify tab host
    private class TabClickListener implements OnClickListener {

        private final int mTabIndex;

        private TabClickListener(int tabIndex) {
            mTabIndex = tabIndex;
        }

        public void onClick(View v) {
            mSelectionChangedListener.onTabSelectionChanged(mTabIndex, true);
        }
    }
    
    static interface OnTabSelectionChanged {
        /**
         * Informs the TabHost which tab was selected. It also indicates
         * if the tab was clicked/pressed or just focused into.
         *
         * @param tabIndex index of the tab that was selected
         * @param clicked whether the selection changed due to a touch/click
         * or due to focus entering the tab through navigation. Pass true
         * if it was due to a press/click and false otherwise.
         */
        void onTabSelectionChanged(int tabIndex, boolean clicked);
    }
}